package campeonatofutebol.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Messenger;
import android.util.Log;
import campeonatofutebol.bean.RepositorioDeDados;
import campeonatofutebol.persistencia.DBAdapter;

public class Servico extends Service {
	
	private Timer timer;
	private TimerTask task;
	private ArrayList<String> favoritados = new ArrayList<String>();
	/*
	class AtualizarFeed extends TimerTask {

		ServicoNotificacao s;
		
		private static final int NOTIFICATION_EX = 1;
		private NotificationManager notificationManager;
		
		public AtualizarFeed(ServicoNotificacao s) {
			this.s = s;
		}
		
		@Override
		public void run() {
			try {
				GerenciadorServico gerenciador = GerenciadorServico.getInstance();
				RepositorioDeDados rd = RepositorioDeDados.getInstance();
				CarregadorDeFeeds carregadorInstance = CarregadorDeFeeds.getInstance();
				if(!favoritados.isEmpty()) {
					String time = favoritados.get(0);
					carregadorInstance.setTimeAtual(time);
					Collection<String> actualUpdate = carregadorInstance.carregaFeed();
					Collection<String> lastUpdate = gerenciador.getUltimaAtualizacao();
					if(lastUpdate != null) {
						if(!lastUpdate.equals(actualUpdate))
							notificar();
					} else
						notificar();
					gerenciador.setUltimaAtualizacao(actualUpdate);
				}
			} catch (Exception e) {
				Log.w("!!!", ""+e.getMessage());
			}
		}
		
		private void notificar() {
		    notificationManager = (NotificationManager) 
		    getSystemService(Context.NOTIFICATION_SERVICE);
		
		    int icon = android.R.drawable.stat_notify_sync;
		    CharSequence tickerText = "Novidades";
		    long when = System.currentTimeMillis();
		
		    Notification notification;
		    notification = new Notification(icon, tickerText, when);
		    Context context = getApplicationContext();
		    CharSequence contentTitle = "Novidades sobre seu time favorito";
		    CharSequence contentText = "Foram recebidas novas noticias sobre o seu time favorito";
		    Intent notificationIntent = new Intent(s, campeonatofutebol.activity.MessageList.class);
		    notificationIntent.putExtra("carregarFavoritos", true);
		    PendingIntent contentIntent = PendingIntent.getActivity(s, 
		        0, notificationIntent, 0);
		
		    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;

		
		    notificationManager.notify(NOTIFICATION_EX, notification);
		    Log.w("!!!", "atualizacao");
		}
		
	}*/
	
	public Servico(){}
	
	@Override
	public IBinder onBind(Intent intent) {
	    return null;
	}
	
	
	@Override   
	public void onCreate() {
		timer = new Timer();
	}   
	
	@Override   
	public void onDestroy() {   
		if(timer != null)
			timer.cancel();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startid) {  
		String servico = intent.getStringExtra("servico");
		
		if(servico.equals("notificacao")) {
			favoritados = intent.getStringArrayListExtra("listaFavoritados");
			if(favoritados == null){
				DBAdapter db = new DBAdapter(getApplicationContext());
				db.open();
				favoritados = (ArrayList<String>) db.getTimesFavoritados();
				db.close();
			}
			long duracao = 60000*GerenciadorServico.getInstance().getDuracaoPausa();
		    timer.scheduleAtFixedRate(new AtualizarFeed(this, favoritados), duracao, duracao);			
		} else if(servico.equals("mural")) {
			long duracao = 1000;
//		    timer.scheduleAtFixedRate(new AtualizarMural(this, ((AdapterHolder)intent.getParcelableExtra("act"))), duracao, duracao);
		    timer.scheduleAtFixedRate(new AtualizarMural(this, ((Messenger)intent.getParcelableExtra("handler")), intent.getStringExtra("tituloJogo")), duracao, duracao);
		}
	    return START_STICKY;
	}

}
